﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Prod;
using ESOL.Common;
using System.IO;
using ESOL.Business.Mst;
using ESOL.WinUI.BaseForm;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmManageBlueprintPrint : BaseForm.frmBaseMdiChildForm
    {
        #region vars
        public List<ProdPlanOrderEntity> ListProdPlanOrder { set; get; }
        public List<ProdPlanOrderItemEntity> ListProdPlanOrderItem { set; get; }
        private Dictionary<string, Image> dictBlueprints = new Dictionary<string, Image>();
        private List<MstProductEntity> listProdProduct = new List<MstProductEntity>();
        #endregion

        #region ctor
        public frmManageBlueprintPrint()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void BindComobox()
        {
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(rcolStatus, "POI", false);
            //tsComoboxPosition.SelectedItem = "左上角";
        }

        private void BindGridMain()
        {

            if (ListProdPlanOrder != null && ListProdPlanOrder.Count > 0)
            {
                StringBuilder Condition = new StringBuilder(" AND PPO.PLAN_ORDER_CODE IN (");
                foreach (ProdPlanOrderEntity m in ListProdPlanOrder)
                {
                    Condition.Append("'" + m.PlanOrderCode + "',");
                }
                Condition.Replace(',', ')', Condition.Length - 1, 1);
                Condition.Append(" AND PPOI.STATUS IN ('POI01','POI03','POI04','POI05','POI06','POI12')");
                Condition.Append(" AND PPOI.PRODUCT_TYPE!='PDTYPE02'");
                Condition.Append(" AND BB.FILE_NAME!='未找到图片'");
                DataTable dt = ProdPlanOrderItemBs.GetDtBlueprintPrintByCondition(Condition.ToString());
                gvMain.DataSource = dt;
            }
            else if (ListProdPlanOrderItem != null && ListProdPlanOrderItem.Count > 0)
            {
                StringBuilder Condition = new StringBuilder(" AND PPOI.PLAN_ORDER_ITEM_ID IN (");
                foreach (ProdPlanOrderItemEntity m in ListProdPlanOrderItem)
                {
                    Condition.Append("'" + m.PlanOrderItemId + "',");
                }
                Condition.Replace(',', ')', Condition.Length - 1, 1);
                Condition.Append(" AND PPOI.STATUS IN ('POI01','POI03','POI04','POI05','POI06','POI12')");
                Condition.Append(" AND PPOI.PRODUCT_TYPE!='PDTYPE02'");
                Condition.Append(" AND BB.FILE_NAME!='未找到图片'");
                DataTable dt = ProdPlanOrderItemBs.GetDtBlueprintPrintByCondition(Condition.ToString());
                gvMain.DataSource = dt;
            }
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComobox();
            BindGridMain();
        }

        //private void tsPrint_Click(object sender, EventArgs e)
        //{
        //    List<ProdPlanOrderItemEntity> listPlanOrderItem = new List<ProdPlanOrderItemEntity>();
        //    listProdProduct = new List<MstProductEntity>();
        //    for (int i = 0; i < gvMain.Rows.Count; i++)
        //    {
        //        bool selected = Convert.ToBoolean(gvMain.Rows[i].Cells["colSelected"].Value);
        //        if (selected)
        //        {
        //            DataRowView dr = (DataRowView)gvMain.Rows[i].DataBoundItem;
        //            string sProductCode = dr["PRODUCT_CODE"].ToString();
        //            MstProductEntity mProduct = MstProductBs.GetProductByProductCode(sProductCode);
        //            if (mProduct != null)
        //            {
        //                listProdProduct.Add(mProduct);
        //            }
        //            ProdPlanOrderItemEntity mPlanOrderItem = ProdPlanOrderItemBs.GetProdPlanOrderItemByID(dr["PLAN_ORDER_ITEM_ID"].ToString());
        //            if (mPlanOrderItem != null)
        //            {
        //                listPlanOrderItem.Add(mPlanOrderItem);
        //            }
        //        }
        //    }
        //    if (listProdProduct.Count <= 0)
        //    {
        //        MessageBox.Show("请选择要打印的编码！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //        return;
        //    }
        //    if (listPlanOrderItem.Count <= 0)
        //    {
        //        MessageBox.Show("请选择要打印的工令单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //        return;
        //    }
        //    using (frmPrintFormBlueprint frm = new frmPrintFormBlueprint())
        //    {
        //        frm.listProduct = listProdProduct;
        //        frm.listPlanOrderItem = listPlanOrderItem;
        //        frm.WaterMarkPosition = tsComoboxPosition.SelectedIndex + 1;
        //        frm.ShowDialog();
        //    }
        //    //frmPrintFormBlueprint frm = new frmPrintFormBlueprint();
        //    //frm.listProduct = listProdProduct;
        //    //frm.listPlanOrderItem = listPlanOrderItem;
        //    //frm.WaterMarkPosition = tsComoboxPosition.SelectedIndex + 1;
        //    //frm.ShowDialog();
        //}

        private void tsClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        //private void gvMain_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        //{
        //    this.gvMain.CommitEdit(DataGridViewDataErrorContexts.Commit);
        //}

        //private void tsBlueprintRecover_Click(object sender, EventArgs e)
        //{
        //    frmProdBlueprintRecover frm = new frmProdBlueprintRecover();
        //    if (frm.ShowDialog() == DialogResult.OK)
        //    {
        //        LoadGridRight();
        //    }
        //}

        #endregion
        private void btnPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmBasePicViewerForm frm = new frmBasePicViewerForm();
            frm.Show();
        }

    }
}
